package ar.edu.uba.fi.jmit.monitorengine.backend.server;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import ar.edu.uba.fi.jmit.monitorengine.Constants;
import ar.edu.uba.fi.jmit.monitorengine.MonitorEngineTestCase;
import ar.edu.uba.fi.jmit.monitorengine.backend.client.JMitFileService;
import ar.edu.uba.fi.jmit.monitorengine.backend.shared.dto.JMitFileDTO;
import ar.edu.uba.fi.jmit.monitorengine.core.ConfigurationManager;
import ar.edu.uba.fi.jmit.monitorengine.parser.ParsersManager;

public class JMitFileServiceTestCase extends MonitorEngineTestCase {
	
	private static String realText = "<jmit>\n" + 
									"<monitorengine>\n" +
									"	<connection protocol=\"http\" ip=\"10.0.0.1\" port=\"8080\" context=\"monitorEngine\"/>\n" +
									"</monitorengine>\n" +
									"<monitor name=\"testInvocationCounter4\" enviroment=\"development\" application=\"Prueba\" type=\"InvocationCounter\">\n" +
									"	<configurations>\n" + 
									"		<configuration name=\"methodConfiguration\">\n" + 
									"			<method name=\"getX\" return=\"java.lang.Integer\" class=\"ar.edu.uba.fi.jmit.aspecter.itegration.testUtils.PointTestInvocationCounter\" id=\"x\">\n" +
									"				<param name=\"data\" type=\"int\"/>\n" + 
									"			</method>\n" + 
									"		</configuration>\n" +
									"	</configurations>\n" +
									"</monitor>\n" +
									"</jmit>\n";

	public void testGetLoadedConfigurationFiles(){
		JMitFileService service = new JMitFileServiceImpl();
		List<JMitFileDTO> files = service.getLoadedConfigurationFiles();
		Collections.sort(files, new Comparator<JMitFileDTO>() {
			@Override
			public int compare(JMitFileDTO o1, JMitFileDTO o2) {
				return o1.getName().toLowerCase().compareTo(o2.getName().toLowerCase());
			}
		});
		assertEquals(6, files.size());
		assertEquals("FieldValue.jmit.xml", files.get(0).getName());
		assertEquals("file.jmit.xml", files.get(1).getName());
		assertEquals("file2.jmit.xml", files.get(2).getName());
		assertEquals("ForTest.jmit.xml", files.get(3).getName());
		assertEquals("InvocationCounter.jmit.xml", files.get(4).getName());
		assertEquals("ManyMonitors.jmit.xml", files.get(5).getName());
	}
	
	public void testDeleteFile() throws IOException{
		JMitFileService service = new JMitFileServiceImpl();
		ParsersManager.loadConfigurationsDefault();
		List<JMitFileDTO> files = service.getLoadedConfigurationFiles();
		Collections.sort(files, new Comparator<JMitFileDTO>() {
			@Override
			public int compare(JMitFileDTO o1, JMitFileDTO o2) {
				return o1.getName().toLowerCase().compareTo(o2.getName().toLowerCase());
			}
		});
		assertEquals(6, files.size());
		assertEquals("FieldValue.jmit.xml", files.get(0).getName());
		assertEquals("file.jmit.xml", files.get(1).getName());
		assertEquals("file2.jmit.xml", files.get(2).getName());
		assertEquals("ForTest.jmit.xml", files.get(3).getName());
		assertEquals("InvocationCounter.jmit.xml", files.get(4).getName());
		assertEquals("ManyMonitors.jmit.xml", files.get(5).getName());
		
		//This line mustn't fail
		assertNotNull(ConfigurationManager.getInstance().getConfiguration("testInvocationCounter4"));
		
		service.deleteFile("ForTest.jmit.xml");
		
		files = service.getLoadedConfigurationFiles();
		Collections.sort(files, new Comparator<JMitFileDTO>() {
			@Override
			public int compare(JMitFileDTO o1, JMitFileDTO o2) {
				return o1.getName().toLowerCase().compareTo(o2.getName().toLowerCase());
			}
		});
		
		assertEquals(5, files.size());
		assertEquals("FieldValue.jmit.xml", files.get(0).getName());
		assertEquals("file.jmit.xml", files.get(1).getName());
		assertEquals("file2.jmit.xml", files.get(2).getName());
		assertEquals("InvocationCounter.jmit.xml", files.get(3).getName());
		assertEquals("ManyMonitors.jmit.xml", files.get(4).getName());
		
		assertNull(ConfigurationManager.getInstance().getConfiguration("testInvocationCounter4"));
		
		BufferedWriter out = new BufferedWriter(new FileWriter(Constants.getConfigurationsDirectory() + "/ForTest.jmit.xml"));
		out.write(realText); //Rewrite the file
		out.close();
		
		files = service.getLoadedConfigurationFiles();
		Collections.sort(files, new Comparator<JMitFileDTO>() {
			@Override
			public int compare(JMitFileDTO o1, JMitFileDTO o2) {
				return o1.getName().toLowerCase().compareTo(o2.getName().toLowerCase());
			}
		});
		assertEquals(6, files.size());
		assertEquals("FieldValue.jmit.xml", files.get(0).getName());
		assertEquals("file.jmit.xml", files.get(1).getName());
		assertEquals("file2.jmit.xml", files.get(2).getName());
		assertEquals("ForTest.jmit.xml", files.get(3).getName());
		assertEquals("InvocationCounter.jmit.xml", files.get(4).getName());
		assertEquals("ManyMonitors.jmit.xml", files.get(5).getName());	
	}
	
	public void testGetFileText(){
		JMitFileService service = new JMitFileServiceImpl();
		String returnedText = "";
		try {
			returnedText = service.getFileText("ForTest.jmit.xml");
		} catch (IOException e){
			fail(e);
		}
		assertEquals(realText, returnedText);
	}
	
}
